package com.crm.jpaandthymeleaf.utils;

import com.crm.jpaandthymeleaf.pool.RedisPool;
import redis.clients.jedis.Jedis;

/**
 * @author 周敬
 * @version 1.0
 * @packageName com.crm.jpaandthymeleaf.utils
 * @fileName RedisUtil
 * @createTime 2021/12/19-21:25-星期日
 * @lastModify 2021/12/19-21:25-星期日
 */
public class RedisUtil {

    /**
     * 设置键为key的过期时间为exTime秒
     * @param key 键
     * @param exTime 过期时间
     * @return
     */
    public static Long expire(String key, int exTime) {
        Jedis jedis = null;
        Long result = null;
        try {
            jedis = RedisPool.getJedis();
            result = jedis.expire(key, exTime);
        } catch (Exception e) {
            e.printStackTrace();
            return result;
        } finally {
            RedisPool.returnResource(jedis);
        }
        return result;
    }

    public static String setEx(String key,String value,int exTime){
        Jedis jedis = null;
        String result = null;
        try {
            jedis = RedisPool.getJedis();
            result = jedis.setex(key,exTime,value);
        } catch (Exception e) {
            e.printStackTrace();
            return result;
        } finally {
            RedisPool.returnResource(jedis);
        }
        return result;
    }

    /**
     * 新建键值对
     * @param key 键
     * @param value 值
     * @return
     */
    public static String set(String key,String value){
        Jedis jedis = null;
        String result = null;
        try {
            jedis = RedisPool.getJedis();
            result = jedis.set(key,value);
        } catch (Exception e) {
            e.printStackTrace();
            return result;
        } finally {
            RedisPool.returnResource(jedis);
        }
        return result;
    }

    /**
     * 获取键为key的数据源
     * @param key 键
     * @return
     */
    public static String get(String key){
        Jedis jedis = null;
        String result = null;
        try {
            jedis = RedisPool.getJedis();
            result = jedis.get(key);
        } catch (Exception e) {
            e.printStackTrace();
            return result;
        } finally {
            RedisPool.returnResource(jedis);
        }
        return result;
    }

    /**
     * 删除键为key的数据源
     * @param key 键
     * @return
     */
    public static Long del(String key){
        Jedis jedis = null;
        Long result = null;
        try {
            jedis = RedisPool.getJedis();
            result = jedis.del(key);
        } catch (Exception e) {
            e.printStackTrace();
            return result;
        } finally {
            RedisPool.returnResource(jedis);
        }
        return result;
    }

    /**
     * 对key的数据源进行加一的操作
     * @param key 键
     * @return
     */
    public static Long incr(String key){
        Jedis jedis = null;
        Long result = null;
        try {
            jedis = RedisPool.getJedis();
            result = jedis.incr(key);
        } catch (Exception e) {
            e.printStackTrace();
            return result;
        } finally {
            RedisPool.returnResource(jedis);
        }
        return result;
    }

    /**
     * 查看键为key所对应的value的数据类型
     * @param key 健
     * @return
     */
    public static String type(String key){
        Jedis jedis = null;
        String result = null;
        try {
            jedis = RedisPool.getJedis();
            result = jedis.type(key);
        } catch (Exception e) {
            e.printStackTrace();
            return result;
        } finally {
            RedisPool.returnResource(jedis);
        }
        return result;
    }

    /**
     * 判断某个健是否存在
     * @param key 键
     * @return
     */
    public static boolean exists(String key){
        Jedis jedis = null;
        boolean result = false;
        try {
            jedis = RedisPool.getJedis();
            result = jedis.exists(key);
        } catch (Exception e) {
            e.printStackTrace();
            return result;
        } finally {
            RedisPool.returnResource(jedis);
        }
        return result;
    }

    //省略其他方法.....

}
